package com.zeny.online.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zeny.online.common.OnlineException;
import com.zeny.online.common.SearchFriendsStatusEnum;
import com.zeny.online.entity.MyFriends;
import com.zeny.online.entity.Users;
import com.zeny.online.mapper.UsersMapper;
import com.zeny.online.service.MyFriendsService;
import com.zeny.online.service.UsersService;
import com.zeny.online.util.MD5Utils;
import com.zeny.online.util.QRCodeUtils;
import com.zeny.online.util.QiniuUtils;
import com.zeny.online.util.SnowFlake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 服务实现类
 * </p>

 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {

    @Autowired
    private QRCodeUtils qrCodeUtils;

    @Autowired
    private MyFriendsService myFriendsService;

    /**
     * @param username
     * @return java.lang.Boolean
     * @Description 根据用户名判断用户是否存在
     **/
    @Override
    public Users findUserByUsername(String username) {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        Users users = baseMapper.selectOne(queryWrapper);
        return users;
    }

    /**
     * @param users
     * @return com.zeny.online.entity.Users
     * @Description 登录
     **/
    @Override
    public Users login(Users users) throws Exception {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", users.getUsername());
        queryWrapper.eq("password", MD5Utils.getMD5Str(users.getPassword()));
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * @param user
     * @return com.zeny.online.entity.Users
     * @Description 注册
     **/
    @Transactional
    @Override
    public Users regist(Users user) throws Exception {
        long l = new SnowFlake(5, 5).nextId();
        String qrcode = "qr_" + l + ".png";
        QiniuUtils.upload2Qiniu(qrCodeUtils.createQRCode("username:" + user.getUsername()), qrcode);
        user.setNickname(user.getUsername());
        user.setFaceImage("face-default-cat.png");
        user.setFaceImageBig("face-default-cat.png");
        user.setQrcode(qrcode);
        user.setPassword(MD5Utils.getMD5Str(user.getPassword()));
        baseMapper.insert(user);
        return user;
    }

    /**
     * @param userId
     * @param friendUsername
     * @return java.lang.Integer
     * @Description 搜索用户
     **/
    @Override
    public Integer searchFriend(String userId, String friendUsername) {
        //验证userId是否是正常用户
        Users users = baseMapper.selectById(userId);
        if (users == null) {
            throw new OnlineException(20002, "用户异常");
        }

        Users friend = null;
        // 1、搜索的用户不存在
        friend = this.findUserByUsername(friendUsername);
        if (friend == null) {
            return SearchFriendsStatusEnum.USER_NOT_EXIST.status;
        }
        // 2、搜索是自己
        if (userId.equals(friend.getId())) {
            return SearchFriendsStatusEnum.NOT_YOURSELF.status;
        }
        // 3、已经是好友
        MyFriends friendMid = myFriendsService.findByMyUserIdAndFriendUserId(userId, friend.getId());
        if (friendMid != null) {
            return SearchFriendsStatusEnum.ALREADY_FRIENDS.status;
        }
        return SearchFriendsStatusEnum.SUCCESS.status;
    }
}
